package com.cch.cooperation.api.support.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.cch.cooperation.api.auth.dto.UserDeviceDTO;
import com.cch.cooperation.api.auth.service.UserDeviceServiceImpl;
import com.cch.cooperation.api.support.dto.PushAppNoticeDTO;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.message.PushChannel;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 消息推送
 *
 * @author cch
 */
@Slf4j
@Service
public class PushServiceImpl {

    @Lazy
    @Resource
    private PushApi pushApi;
    @Resource
    private UserDeviceServiceImpl userDeviceService;

    public void pushAppNotice(PushAppNoticeDTO req) {
        // 推送到客户端
        PushDTO<Audience> pushReq = new PushDTO<>();
        pushReq.setRequestId(req.getId());
        // 设置推送信息
        PushMessage pushMessage = new PushMessage();
        GTNotification notification = new GTNotification();
        notification.setTitle(req.getTitle());
        notification.setBody(req.getBody());
        // 拼接title和body参数
        if (StrUtil.isEmpty(req.getUrl())) {
            notification.setClickType("startapp");
        } else {
            notification.setClickType("payload");
            notification.setPayload(JSON.toJSONString(req));
        }
        pushMessage.setNotification(notification);
        pushReq.setPushMessage(pushMessage);
        // 设置ios渠道信息
        IosDTO ios = new IosDTO();
        ios.setApnsCollapseId(req.getId());
        Aps aps = new Aps();
        ios.setAps(aps);
        if (StrUtil.isNotEmpty(req.getUrl())) {
            ios.setPayload(JSON.toJSONString(req));
        }
        Alert alert = new Alert();
        aps.setAlert(alert);
        alert.setTitle(req.getTitle());
        alert.setBody(req.getBody());
        PushChannel pushChannel = new PushChannel();
        pushChannel.setIos(ios);
        pushReq.setPushChannel(pushChannel);
        // 设置推送人
        Audience audience = new Audience();
        UserDeviceDTO userDevice = userDeviceService.get(req.getUserId());
        if (Objects.isNull(userDevice) || StrUtil.isEmpty(userDevice.getCid())) {
            log.info("个推推送失败，cid为空 req:{}, userDevice:{}", pushReq, userDevice);
            return;
        }
        audience.setCid(Lists.newArrayList(userDevice.getCid()));
        pushReq.setAudience(audience);
        // 推送
        ApiResult resp = pushApi.pushToSingleByCid(pushReq);
        log.info("个推推送发出 req:{}, resp:{}", pushReq, resp);
    }

}
